﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace SISTEMA_ALPROVIDE
{



    public partial class fmProducto : Form
    {

        public fmProducto()
        {
            InitializeComponent();
            fillDataGrid();
            this.TopMost = true;
        }

        //LLENA TABLA CON LOS DATOS DE LOS PRODUCTOS DE LA BD:
        public void fillDataGrid()
        {
            //string ConnectionString = "Server=aries.lab.inf.pucp.edu.pe;Database=m20067005;Uid=m20067005;Pwd=u6c7qm;Convert Zero Datetime=True";
            string ConnectionString = "Server=50.87.33.40;Database=pardoyel_loitay;Uid=pardoyel_loitay;Pwd=loitay123;Convert Zero Datetime=True";
            MySqlConnection connection;
            connection = new MySqlConnection(ConnectionString);
            connection.Open();

            String strsql = "SELECT * FROM producto WHERE estado=1";

            DataSet ds = new DataSet();
            MySqlDataAdapter adapter = new MySqlDataAdapter(strsql, connection);
            adapter.Fill(ds);
            gridProducto.DataSource = ds.Tables[0];
        }


        
        public void registrarProducto(String tipo, String nombProd, String marca, double precio, String UVenta, double peso, String FechFab, String FechCaduc, int stock, int estado){
            int idTipProd=0;

            switch(tipo){
                case ("PAPELERIA"): idTipProd= 1001;
                                    break;
                case ("CUADERNO"): idTipProd= 1002;
                                    break;
                case ("LAPICERO"): idTipProd= 1003;
                                    break;
            }



            //string ConnectionString = "Server=aries.lab.inf.pucp.edu.pe;Database=m20067005;Uid=m20067005;Pwd=u6c7qm;";
            string ConnectionString = "Server=50.87.33.40;Database=pardoyel_loitay;Uid=pardoyel_loitay;Pwd=loitay123;Convert Zero Datetime=True";
            MySqlConnection connection;
            connection = new MySqlConnection(ConnectionString);
            
            connection.Open();
               
                MySqlCommand command = new MySqlCommand("INSERT INTO producto values( null" + ",'" + nombProd + "', "
                + peso + "," + precio + ", '" + FechFab + "', '" + FechCaduc + "', '" + marca + "' ," + idTipProd + ", '"
                + UVenta + "' ," + stock + ","+  estado + ")" , connection);

                command.ExecuteNonQuery();
            connection.Close();

            fillDataGrid();
            
        }




        private void btnGuardar_Click_1(object sender, EventArgs e)
        {
            String fechaIng = cmbFFab.Value.Date.ToString("yyyy-MM-dd");
            String fechaCad = cmbFCad.Value.Date.ToString("yyyy-MM-dd");

            registrarProducto(cmbTipo.Text, txtNomb.Text, cmbMarca.Text, Convert.ToDouble(txtPrec.Text), txtUVenta.Text, Convert.ToDouble(txtPeso.Text), fechaIng, fechaCad, Convert.ToInt32(txtStock.Text), Convert.ToInt32(txtEstado.Text));
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        //LIMPIAR CELDAS:
        private void btnRLimpiar_Click(object sender, EventArgs e)
        {
            cmbTipo.Text= "";
            txtNomb.Text= "";
            cmbMarca.Text= "";
            txtPrec.Text= "";
            txtUVenta.Text= "";
            txtPeso.Text= "";
            cmbFFab.Text= DateTime.Now.ToString();
            cmbFCad.Text= DateTime.Now.ToString();
            txtStock.Text = "";
            txtEstado.Text = "";
        }

        //COMPLETAR LOS CAMPOS AL SELECCIONAR UNA FILA CON 2 CLICK:
        private void gridProducto_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int tipo=0;
            String tipoProd="";

            DataGridViewRow fila = gridProducto.SelectedRows[0];
            tipo = Convert.ToInt32(fila.Cells[7].Value);
            
            switch (tipo)
            {
                case (1001): tipoProd = "PAPELERIA";
                    break;
                case (1002): tipoProd = "CUADERNO";
                    break;
                case (1003): tipoProd = "LAPICERO";
                    break;
            }

            String fechaFab = cmbFCad.Value.Date.ToString("yyyy-MM-dd");

            cmbTipo.Text = tipoProd;
            txtNomb.Text = Convert.ToString(fila.Cells[1].Value);
            cmbMarca.Text = Convert.ToString(fila.Cells[6].Value);
            txtPrec.Text = Convert.ToString(fila.Cells[3].Value);
            txtUVenta.Text = Convert.ToString(fila.Cells[8].Value);
            txtPeso.Text = Convert.ToString(fila.Cells[2].Value);
            //cmbFFab.Text = DateTime.Now.ToString(); //Convert.ToString(fila.Cells[4].Value);
            //cmbFCad.Text = DateTime.Now.ToString(); //Convert.ToString(fila.Cells[5].Value);
            txtStock.Text = Convert.ToString(fila.Cells[9].Value);
            txtEstado.Text = Convert.ToString(fila.Cells[10].Value);
        }

        //ACTUALIZAR:
        private void button1_Click(object sender, EventArgs e)
        {
            DataGridViewRow fila = gridProducto.SelectedRows[0];
            int codBuscado = 0;

            codBuscado = Convert.ToInt32(fila.Cells[0].Value);

            String fechaIng = cmbFFab.Value.Date.ToString("yyyy-MM-dd");
            String fechaCad = cmbFCad.Value.Date.ToString("yyyy-MM-dd");

            int idTipo = Convert.ToInt32(fila.Cells[7].Value);

            actualizarProducto(codBuscado, idTipo, txtNomb.Text, cmbMarca.Text, Convert.ToDouble(txtPrec.Text), txtUVenta.Text, Convert.ToDouble(txtPeso.Text), fechaIng, fechaCad, Convert.ToInt32(txtStock.Text), Convert.ToInt32(txtEstado.Text));
        }

        private void actualizarProducto(int codBuscado, int idTipo, String nombProd, String marca, double precio, String UVenta, double peso, String FechFab, String FechCaduc, int stock, int estado)
        {
            
            //string ConnectionString = "Server=aries.lab.inf.pucp.edu.pe;Database=m20067005;Uid=m20067005;Pwd=u6c7qm;";
            string ConnectionString = "Server=50.87.33.40;Database=pardoyel_loitay;Uid=pardoyel_loitay;Pwd=loitay123;Convert Zero Datetime=True";
            MySqlConnection connection;
            connection = new MySqlConnection(ConnectionString);

            connection.Open();

                MySqlCommand command = new MySqlCommand("UPDATE producto SET " + 
                    "nombre= '" + nombProd + "', " +
                    "peso=" + peso + ", " +
                    "precio= " + precio + ", " +
                    //"fecha_fabricacion=" + FechFab + ", " +
                    //"fecha_vencimiento=" + FechCaduc + ", " +
                    "marca='" + marca + "', " +
                    "idTipoProd= " + idTipo + ", " +
                    "uni_venta= '" + UVenta + "', " +
                    "stock=" + stock + ", " +
                    "estado=" + estado + 
                    " WHERE idProducto= " + codBuscado, connection);
                command.ExecuteNonQuery();
            connection.Close();

            fillDataGrid();
        }


        private void btnEliminar_Click(object sender, EventArgs e)
        {
            DataGridViewRow fila = gridProducto.SelectedRows[0];
            int codElim= 0;
            codElim= Convert.ToInt32(fila.Cells[0].Value);
            eliminarProducto(codElim);

        }

        private void eliminarProducto(int codEliminar)
        {
            //string ConnectionString = "Server=aries.lab.inf.pucp.edu.pe;Database=m20067005;Uid=m20067005;Pwd=u6c7qm;";
            string ConnectionString = "Server=50.87.33.40;Database=pardoyel_loitay;Uid=pardoyel_loitay;Pwd=loitay123;Convert Zero Datetime=True";
            MySqlConnection connection;
            connection = new MySqlConnection(ConnectionString);

            connection.Open();
                MySqlCommand command = new MySqlCommand("UPDATE producto SET estado= 0 WHERE idProducto=" + codEliminar, connection);
                command.ExecuteNonQuery();
            connection.Close();

            fillDataGrid();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            int codigoBus= 0;
            //string nombre = txtCodBus.Text;
            if (txtCodBus.Text != "")
                codigoBus = Convert.ToInt32(txtCodBus.Text);
            buscarProducto(codigoBus, cmbTipoBus.Text);

        }

        private void buscarProducto(int codBus, String tipoBus)
        {
            string ConnectionString = "Server=50.87.33.40;Database=pardoyel_loitay;Uid=pardoyel_loitay;Pwd=loitay123;Convert Zero Datetime=True";
            
            String strCodBus = "";
            if (codBus != 0)
                strCodBus = Convert.ToString(codBus);
            else
                strCodBus = "";
            
            MySqlConnection connection;
            connection = new MySqlConnection(ConnectionString);

            connection.Open();

            //Búsqueda por NOMBRE:
            if ((strCodBus != "") && (cmbTipoBus.Text == ""))
            {
                String strsql = "SELECT * FROM producto WHERE idProducto= " + codBus;
                DataSet ds = new DataSet();
                MySqlDataAdapter adapter = new MySqlDataAdapter(strsql, connection);
                adapter.Fill(ds);
                gridProducto.DataSource = ds.Tables[0];
            }

            //Búsqueda por TIPO:
            if ((strCodBus == "") && (cmbTipoBus.Text != ""))
            {
                int idTipProd = 0;

                switch (cmbTipoBus.Text)
                {
                    case ("PAPELERIA"): idTipProd = 1001;
                        break;
                    case ("CUADERNO"): idTipProd = 1002;
                        break;
                    case ("LAPICERO"): idTipProd = 1003;
                        break;
                }

                String strsql = "SELECT * FROM producto WHERE idTipoProd= " + idTipProd;
                DataSet ds = new DataSet();
                MySqlDataAdapter adapter = new MySqlDataAdapter(strsql, connection);
                adapter.Fill(ds);
                gridProducto.DataSource = ds.Tables[0];
            }

        }

    }
}
